import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-12
 * Time: 20:02
 * author:JdbcDelete
 */
public class JdbcDelete {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.用户输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除的姓名:");
        String name = scanner.next();

        //4.操作数据库
        String sql = "delete from student where name= ?";
        PreparedStatement statement = connection.prepareStatement( sql);
        statement.setString(1,name);

        //5.执行
        int ret =  statement.executeUpdate();
        System.out.println( ret );

        //6.关闭资源
        statement.close();;
        connection.close();
    }
}
